package com.sfc.sdsr.admin;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.UUID;

import org.apache.log4j.Logger;

import com.sfc.sdsr.common.model.Address;
import com.sfc.sdsr.common.model.Event;
import com.sfc.sdsr.common.model.Key;
import com.sfc.sdsr.common.model.News;
import com.sfc.sdsr.common.model.Occupation;
import com.sfc.sdsr.common.model.Person;
import com.sfc.sdsr.common.model.SfcRole;
import com.sfc.sdsr.common.model.Song;

public class MainAdmin {

	private final Logger log = Logger.getLogger(getClass());
	private final String SERVER = "localhost";
	private final int PORT = 4444;
	
	public static void main(String[] args) {
		MainAdmin main = new MainAdmin();
		main.start();
	}

	private void start() {
        ServerAccess serverAccess = new ServerAccess(SERVER, PORT);

        Address address = new Address();
        address.setHouseNumber(100);
        address.setStreetName(UUID.randomUUID().toString().substring(0,4));
        address.setModifier(UUID.randomUUID().toString().substring(0,4));
        address.setAddressUuid(UUID.randomUUID());
        
        Person person = new Person();
        person.setFirstName(UUID.randomUUID().toString().substring(0,4));
        person.setLastName(UUID.randomUUID().toString().substring(0,4));
        person.setAddress(address);
        person.setPersonUuid(UUID.randomUUID());
        person.setOccupation(Occupation.STUDENT);
        person.setSfcRole(SfcRole.MEMBER);
        
        News news = new News();
        news.setAuthor(person);
        news.setUtcReportDate(new Date().getTime());
        news.setHeadline(UUID.randomUUID().toString().substring(0,4));
        news.setAuthor(person);
        news.setNewsUuid(UUID.randomUUID());
        
        Song song = new Song();
        song.setTitle(UUID.randomUUID().toString().substring(0,4));
        song.setSongUuid(UUID.randomUUID());
        song.setKeySignature(Key.A);
        
        Event event = new Event();
        event.setEventName(UUID.randomUUID().toString().substring(0,4));
        event.setUtcEventDate(new Date().getTime());
        event.setAddress(address);
        event.setOrganizer(person);
        event.setEventUid(UUID.randomUUID());
        try {
            event.setWebpage(new URL("http://www.google.com"));
        } catch (MalformedURLException mue) {
            log.error("mue: " + mue.getMessage());
        }
        
        try {
            log.info("Creating Address...");
            if (serverAccess.createAddress(address)) {
                log.info("create Address success");
            } else {
                log.info("create Address fail");
            }
            
            log.info("Creating Member...");
            if (serverAccess.createMember(person)) {
                log.info("create Member success");
            } else {
                log.info("create Member fail");
            }

            log.info("Creating News...");
            if (serverAccess.createNews(news)) {
                log.info("Create News success");
            } else {
                log.info("Create News fail");
            }

            log.info("Creating Event...");
            if (serverAccess.createEvent(event)) {
                log.info("Create Event success");
            } else {
                log.info("Create Event fail");
            }

            log.info("Creating Song...");
            if (serverAccess.createSong(song)) {
                log.info("create Song success");
            } else {
                log.info("create Song fail");
            }
        } catch (IOException ioe) {
            log.error("Failed to connect to server, check that " + SERVER + ":" + PORT + " is ping-able");
        }
	}

}
